﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Windows.Forms;

public partial class shopCart : System.Web.UI.Page
{
    string strconn = ConfigurationManager.ConnectionStrings["a123"].ToString();
    SqlConnection sqlconn;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
            
        }
    }
    public void bind()
    {
        string number;
        int num;
        int je;
        int ii;
        sqlconn = new SqlConnection(strconn);
        sqlconn.Open();
        string sqlstr = "select * from tb_Shopping where UserName='" + Session["ID"] + "'";
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlconn);
        DataSet myds = new DataSet();
        myda.Fill(myds, "tb_Shopping");
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "BookID" };
        GridView1.DataBind();
        sqlconn.Close();
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            System.Web.UI.WebControls.Label label = (System.Web.UI.WebControls.Label)GridView1.Rows[i].FindControl("Label2");
            System.Web.UI.WebControls.DropDownList dropDownList = (System.Web.UI.WebControls.DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
            
            number = GridView1.Rows[i].Cells[3].Text;
            je=Convert.ToInt32(number);
            num = Convert.ToInt32(dropDownList.SelectedValue);
            ii = je * num;
            label.Text=ii.ToString();
        }
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            System.Web.UI.WebControls.CheckBox checkbox = (System.Web.UI.WebControls.CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (CheckBox2.Checked == true)
            {
                checkbox.Checked = true;
            }
            else { checkbox.Checked = false; }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DialogResult result = MessageBox.Show("请确认是否要移除选中的记录行？", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.ServiceNotification);
        if (result == DialogResult.OK)
        {
            sqlconn = new SqlConnection(strconn);
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                System.Web.UI.WebControls.CheckBox checkbox = (System.Web.UI.WebControls.CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                if (checkbox.Checked == true)
                {
                    string sqlstr = "delete from tb_Shopping where BookID='" + GridView1.DataKeys[i].Value + "' and UserName='" + Session["ID"] + "'";
                    sqlconn.Open();
                    SqlCommand sqlcom = new SqlCommand(sqlstr, sqlconn);
                    sqlcom.ExecuteNonQuery();
                    sqlconn.Close();
                }
            }
            bind();
        }
        else { DialogResult.Cancel.Equals("true"); }
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        //Rows--第几行
        //Cells--第几列---获取GridView1中的值
        //string number = GridView1.Rows[i].Cells[3].Text;
        DateTime datetime = DateTime.Now;
        DialogResult result = MessageBox.Show("请确认是否要购买选中的记录行？", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.ServiceNotification);
        if (result == DialogResult.OK)
        {
            sqlconn = new SqlConnection(strconn);
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                System.Web.UI.WebControls.CheckBox checkbox = (System.Web.UI.WebControls.CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                if (checkbox.Checked == true)
                {
                    //bookId = GridView1.DataKeys[i].Value;
                    System.Web.UI.WebControls.Label L = (System.Web.UI.WebControls.Label)GridView1.Rows[i].FindControl("Label1");
                    System.Web.UI.WebControls.DropDownList drpo1 = (System.Web.UI.WebControls.DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
                    string number = drpo1.SelectedValue;
                    
                    string sqlstr = "insert into tb_OrderInfo values('" + Session["ID"] + "','" + GridView1.DataKeys[i].Value + "','" + L.Text + "','"+GridView1.Rows[i].Cells[3].Text+"','" + number + "','false','false',null,null,null,null,null)";
                    string sqlstr1 = "delete from tb_Shopping where BookID='" + GridView1.DataKeys[i].Value + "' and UserName='" + Session["ID"] + "'";
                    sqlconn.Open();
                    SqlCommand sqlcom = new SqlCommand(sqlstr, sqlconn);
                    sqlcom.ExecuteNonQuery();
                    SqlCommand sqlcom1 = new SqlCommand(sqlstr1, sqlconn);
                    sqlcom1.ExecuteNonQuery();
                    sqlconn.Close();
                    //Response.Write(number + L.Text);
                    
                }
            }
            Response.Redirect("checkOut.aspx");
            //bind();
        }
        else { DialogResult.Cancel.Equals("true"); }
        
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        sqlconn = new SqlConnection(strconn);
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            System.Web.UI.WebControls.DropDownList dropDownList = (System.Web.UI.WebControls.DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
            int num=Convert.ToInt32(dropDownList.SelectedValue);
            string sqlstr = "update tb_Shopping set Number='"+num+"' where BookID='"+GridView1.DataKeys[i].Value+"' and UserName='"+Session["ID"]+"'";
            sqlconn.Open();
            SqlCommand sqlcom = new SqlCommand(sqlstr, sqlconn);
            sqlcom.ExecuteNonQuery();
            sqlconn.Close();
        }
        bind();
    }
}